package com.tzb.ssm.controller.admin;

import com.tzb.ssm.entity.admin.Menu;
import com.tzb.ssm.page.admin.Page;
import com.tzb.ssm.service.admin.MenuService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 菜单管理控制器
 * @Author tzb
 * @Date 2021/6/6 16:46
 * @Version 1.0
 **/

@RequestMapping("/admin/menu")
@Controller
public class MenuController {


    @Autowired
    private MenuService menuService;

    /**
     * 菜单管理列表页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model) {
        model.addObject("topList", menuService.findTopList());
        model.setViewName("menu/list");
        return model;
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getMenuList(Page page,
                                           @RequestParam(name = "name", required = false, defaultValue = "") String name) {
        Map<String, Object> ret = new HashMap<>();

        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        queryMap.put("name", name);

        List<Menu> list = menuService.findList(queryMap);

        ret.put("rows", list);
        ret.put("total", menuService.getTotal(queryMap));

        return ret;
    }


    @RequestMapping(value = "/get_icons", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getIconList(HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<>();
        String realPath = request.getServletContext().getRealPath("/");

        File file = new File(realPath + "\\resources\\admin\\easyui\\css\\icons");

        List<String> icons = new ArrayList<>();

        if (!file.exists()) {
            ret.put("type", "error");
            ret.put("msg", "文件目录不存在");
            return ret;
        }
        for (File f : file.listFiles()) {
            if (f != null && f.getName().contains("png")) {
                icons.add("icon-" + f.getName().substring(0, f.getName().lastIndexOf(".")).replace("_","-"));
            }
        }

        ret.put("type", "success");
        ret.put("content", icons);

        return ret;
    }


    /**
     * 添加菜单
     *
     * @param menu
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Menu menu) {
        Map<String, String> ret = new HashMap<>();

        if (menu == null) {
            ret.put("type", "error");
            ret.put("msg", "请填写正确的菜单信息");
            return ret;
        }

        if (StringUtils.isEmpty(menu.getName())) {
            ret.put("type", "error");
            ret.put("msg", "请填写菜单信息");
            return ret;
        }

        if (menu.getParentId() == null) {
            menu.setParentId(-1L);
        }

        if (StringUtils.isEmpty(menu.getIcon())) {
            ret.put("type", "error");
            ret.put("msg", "请填写菜单图标类");
            return ret;
        }

        if (menuService.add(menu) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "添加失败，请联系管理员");
            return ret;
        }

        ret.put("type", "success");
        ret.put("msg", "添加成功");
        return ret;
    }

}
